package cn.yunhe.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.yunhe.biz.IUniverstityBiz;
import cn.yunhe.biz.UniverstityBizImpl;
import cn.yunhe.entity.University;

/**
 * Servlet implementation class UniverstityServlet
 */
@WebServlet("/UniverstityServlet")
public class UniverstityServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		String nnef = request.getParameter("nef");
		String ttoo = request.getParameter("too");
		String ccom = request.getParameter("com");
		
		String add = request.getParameter("add");
		String delete = request.getParameter("delete");
		String update = request.getParameter("update");
		
	
		IUniverstityBiz univerBiz = new UniverstityBizImpl();
		HttpSession session = request.getSession();
		
		
		if ("1".equals(nnef)) {
			List<University> listisnef=univerBiz.searchIsnef();
			
			if(listisnef !=null){
				session.setAttribute("list", listisnef);
				request.getRequestDispatcher("University.jsp").forward(request, response);
			} 
		} else if ("1".equals(ttoo)) {
			List<University> lististoo=univerBiz.searchIstoo();
			if(lististoo !=null){
				session.setAttribute("list", lististoo);
				request.getRequestDispatcher("University.jsp").forward(request, response);
			}
		} else if ("1".equals(ccom)) {
			List<University> listisComm=univerBiz.searchCommon();
			if(listisComm !=null){
				session.setAttribute("list", listisComm);
				request.getRequestDispatcher("University.jsp").forward(request, response);
			}
		}else if("1".equals(add)){
			String name = request.getParameter("name");
			String isnef = request.getParameter("isnef");
			String istoo = request.getParameter("istoo");
			String contact = request.getParameter("contact");
			String url = request.getParameter("url");
			University university =new University();
			university.setIsnef(Integer.valueOf(isnef));
			university.setIstoo(Integer.valueOf(istoo));
			university.setUname(name);
			university.setContacts(contact);
			university.setUrl(url);
			boolean flag= univerBiz.addUniversity(university);
			if(flag){
				request.getRequestDispatcher("###").forward(request, response);
			}
		}else if("1".equals(delete)){
			int uid=Integer.valueOf(request.getParameter("universityid"));
			
			boolean flag= univerBiz.deleteUniById(uid);
			if(flag){
				request.getRequestDispatcher("###").forward(request, response);
			}
		}else if("1".equals(update)){
			String id = request.getParameter("universityid");
			String name = request.getParameter("name");
			String isnef = request.getParameter("isnef");
			String istoo = request.getParameter("istoo");
			String contact = request.getParameter("contact");
			String url = request.getParameter("url");
			University university =new University();
			university.setIsnef(Integer.valueOf(isnef));
			university.setIstoo(Integer.valueOf(istoo));
			university.setUname(name);
			university.setContacts(contact);
			university.setUrl(url);
			university.setUniversityid(Integer.valueOf(id));
			boolean flag=univerBiz.updateUniById(university);
			if(flag){
				request.getRequestDispatcher("###").forward(request, response);
			}
		}	
		
		
		
		
		
	
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
